﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
namespace OnLineExam.Web.mag.Handler
{
    /// <summary>
    /// pxCourseHandler 的摘要说明
    /// </summary>
    public class pxCourseHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string type = context.Request.Params["type"];

            var ID = context.Request.QueryString["ID"];

            OnLineExam.BLL.px_Course bll = new OnLineExam.BLL.px_Course();

            switch (type)
            {
                case "getlist":
                    #region 获取列表
                    int page = int.Parse(context.Request.Params["page"]);
                    int rows = int.Parse(context.Request.Params["rows"]);
                    string depid = context.Request.Params["depid"];
                    int startIndex = (page - 1) * rows + 1;
                    int endIndex = (page) * rows;

                    //var loginuser=new UserManager().GetModel(context.User.Identity.Name);
                    var ds = bll.GetListByPage("depid=" + depid, "", startIndex, endIndex);
                    int RecordCount = bll.GetRecordCount("depid=" + depid);
                    context.Response.Write(HHG.Common.Json.DataTable2Json(ds.Tables[0], RecordCount));
                    #endregion
                    break;
                case "edit":
                    #region 根据ID获取信息

                    if (!string.IsNullOrEmpty(ID))
                    {
                        var m = bll.GetModel(int.Parse(ID));
                        //m.c_startDate = m.c_startDate.ToString("yyyy-MM-dd");

                        //var rsult = HHG.Common.Json.GetJson<OnLineExam.Model.px_Course>(m);
                        JavaScriptSerializer json = new JavaScriptSerializer();
                       var rsult = json.Serialize(m);
                        rsult = Regex.Replace(rsult, @"\\/Date\((\d+)\)\\/", match =>
                        {
                            DateTime dt = new DateTime(1970, 1, 1);
                            dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
                            dt = dt.ToLocalTime();
                            return dt.ToString("yyyy-MM-dd");
                        });
                        context.Response.Write(rsult);
                    }
                    
                    #endregion
                    break;
                case "addsave":
                    #region 提交修改
                    depid = context.Request.Params["selectDep"];
                   
                    var new_model = new OnLineExam.Model.px_Course();
                    new_model.c_title = context.Request.Params["txtTitle"];
                    new_model.DepID = int.Parse(depid);
                    new_model.ClassID = int.Parse(context.Request.Params["selectClass"]);
                    new_model.c_content = context.Request.Params["txtContent"];
                    new_model.c_type = context.Request.Params["selectType"];
                    new_model.c_path = context.Request.Params["txtPath"];
                     
                    new_model.c_startDate = DateTime.Parse(context.Request.Params["txtstartDate"]);
                    new_model.c_endDate = DateTime.Parse(context.Request.Params["txtendDate"]);
                    new_model.alowUserJobType = context.Request.Params["txtUserJobType"];
                    new_model.alowUserID = context.Request.Params["txtalowUserID"];
                    new_model.PaperID =int.Parse( context.Request.Params["selectPaper"]);
                    new_model.completeDate = DateTime.Now;
                    new_model.completeUserID = "0";
                    new_model.c_addtime = DateTime.Now;
                    int nID = bll.Add(new_model);
                    context.Response.Write(nID);//修改成功，返回ID,失败，返回0

                    #endregion
                    break;
                case "editsave":
                    #region 提交修改
                    depid = context.Request.Params["selectDep"];
                    ID = context.Request.Params["hdID"];
                    var o_model = bll.GetModel(int.Parse(ID));
                    o_model.c_title = context.Request.Params["txtTitle"];
                    o_model.DepID = int.Parse(depid);
                    o_model.ClassID = int.Parse(context.Request.Params["selectClass"]);
                    o_model.c_content = context.Request.Params["txtContent"];
                    o_model.c_type = context.Request.Params["selectType"];
                    o_model.c_path = context.Request.Params["txtPath"];
                     
                    o_model.c_startDate = DateTime.Parse(context.Request.Params["txtstartDate"]);
                    o_model.c_endDate = DateTime.Parse(context.Request.Params["txtendDate"]);
                    o_model.alowUserJobType = context.Request.Params["txtUserJobType"];
                    o_model.alowUserID = context.Request.Params["txtalowUserID"];
                    o_model.PaperID = int.Parse(context.Request.Params["selectPaper"]);
                    o_model.c_addtime = DateTime.Now;



                    if (bll.Update(o_model))
                    {
                        context.Response.Write(ID);//修改成功，返回ID
                    }
                    else
                    {
                        context.Response.Write("0");//修改失败，返回0
                    }
                    #endregion
                    break;
                case "del":
                    #region 删除
                    ID = context.Request.Params["ID"];
                    if (bll.Delete(int.Parse(ID)))
                    {
                        context.Response.Write(ID);//删除成功，返回ID
                    }
                    else
                    {
                        context.Response.Write("0");//删除失败，返回0
                    }
                    #endregion
                    break;
                
                default:
                    break;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}